<?php
/*************************************************************************************
 *
 *	FileName: CUploader.php
 *	Desc    : manage the upload of the files
 *	Author  : Seal
 *	Date    : 2010-1-14
 *
 *************************************************************************************/

class CUploader {
	private $permitted_file = array( 'jpg', 'jpeg', 'gif', 'png', 'bmp' );

	public function SaveFiles( &$pic_names, &$uploaded_files, &$errmsg, $model_name )
	{
		
		$edpimage = GetEDPImage();
		
		if( isset( $_FILES["files"] ) ) 
		{
			$updir = DIR_UPLOAD_FILE;

			$upload_file = $_FILES["files"];

			date_default_timezone_set('Asia/Shanghai');

			$i = 0;

			// Check the size of each file does not exceed the maximum size
			// Check whether these files satisfy the policy we defined

			foreach( $upload_file['name'] as $key => $value ) 
			{
				if( trim($value) == '' )
					continue;
					
				if( $upload_file["error"][$key] == 1 || $upload_file['error'][$key] == 2 ) 
				{
					$errmsg->add_session( $model_name, "一个评论的所有上传图片不能超过2M 大小！" );
					break;
				}
				
				// important! check the file is really a picture, not other file of types
				if( $edpimage->IsValidPicture($upload_file["tmp_name"][$key]) == false )
				{
					$errmsg->add_session( $model_name, "上传文件：" . $upload_file['name'][$key] . ' 是非法的图片格式！' );
					continue;
				}

				if( edp_is_empty_string( $upload_file["name"][$key] ) )
					continue;

				$file_info = pathinfo( $upload_file["name"][$key] );
				$file_ext = EdpToLower($file_info["extension"]);

				if( !in_array( $file_ext, $this->permitted_file ) ) 
				{
					$errmsg->add_session( $model_name, '上传文件：' . $upload_file['name'][$key] . ' 不符合规范！' );
					continue;
				}

				if( !is_uploaded_file($upload_file["tmp_name"][$key] ) || $upload_file["error"][$key] != 0 ) {
					$errmsg->add_session( $model_name, '上传文件： ' . $upload_file['name'][$key] . ' 没有成功，请稍后再试！');
					continue;
				}

				if( edp_is_empty_string($pic_names[$i]) )
					$pic_names[$i] = $upload_file['name'][$key];

				$i++;
			}

			// Any error existed, return and go back to the user
			if( $errmsg->size($model_name) > 0 )
				return false;

		

			// For each file, copy it to the proper folder
			$i = 0;
			foreach( $upload_file['name'] as $key => $value ) 
			{
				if( trim($value) == '' )
					continue;
					
				$m_dir = date('Y') . '/' . date('m') . '/' . date('d') . '/' . date('G') . '/';
				$upload_path = $updir . $m_dir;
				$shortPath = $m_dir;

				$this->CreateDir( $upload_path );

				$upload_file['name'][$key] = trim( $upload_file['name'][$key] );

				list( $usec, $sec ) = explode( " " , microtime() );
				$upload_file['name'][$key] = substr( $usec, 2 ) . '.' . $file_ext;

				$upload_file['filename'][$key] = $upload_path . $upload_file['name'][$key];
				$shortPath .= $upload_file['name'][$key];

				move_uploaded_file( $upload_file["tmp_name"][$key], $upload_file["filename"][$key] );

				$edpimage->SavePicture( $upload_file["filename"][$key] );

				// if the original file is not jpg, then delete the original file 
				if( preg_match('/\.jpg$/i', $upload_file["filename"][$key]) == 0 ) 
				{
					@unlink( $upload_file["filename"][$key] );
					$shortPath = preg_replace( "/\..+$/i", ".jpg", $shortPath );
				}

				//echo "<img src='" . STATIC_DOMAIN . $upload_file['filename'][$key] . "' />" ;

				$uploaded_files[] = array(
					"path" => $shortPath,
					"name" => edp_db_input($pic_names[$i])
				);
				$i++;
			}

		}
		
		return true;
	}

	public function SaveUserAvatar( $userid, &$errmsg, $model_name ) {
		
		if( isset( $_FILES["avatar"] ) && !edp_is_empty_string( $_FILES['avatar']['name'] ) ) 
		{
			$edpimage = GetEDPImage();
			$updir = DIR_AVATAR;
			$upload_file = $_FILES["avatar"];
			date_default_timezone_set('Asia/Shanghai');
			
			// Check the size of each file does not exceed the maximum size
			// Check whether these files satisfy the policy we defined
			if( $upload_file["error"] == 1 || $upload_file['error'] == 2 ) 
				$errmsg->add( $model_name, '上传头像图片不能超过2M 大小' );

			// important! check the file is really a picture, not other file of types
			if( $edpimage->IsValidPicture($upload_file["tmp_name"]) == false )
				$errmsg->add_session( $model_name, "上传文件：" . $upload_file['name'] . ' 是非法的图片格式！' );
			
				$file_info = pathinfo( $upload_file["name"] );
			$file_ext = $file_info["extension"];

			if( !in_array(strtolower( $file_ext ), $this->permitted_file ) ) 
				$errmsg->add( $model_name, '上传文件: ' . $upload_file['name'] . ' 不符合规范!' );

			if( !is_uploaded_file($upload_file["tmp_name"] ) || $upload_file["error"] != 0 ) 
				$errmsg->add( $model_name, '上传文件: ' . $upload_file['name'] . ' 没有成功，请稍后再试!');
		}

		// Any error existed, return and go back to the user
		if( $errmsg->size($model_name) > 0 )
			return false;

		$edpimage = GetEDPImage();

		$m_dir = date('Y') . '/' . date('m') . '/' . date('d') . '/';
		$upload_path = $updir . $m_dir;
		

		$this->CreateDir( $upload_path );

		$upload_file['name'] = trim( $upload_file['name'] );
		list( $usec, $sec ) = explode( " " , microtime() );
		$upload_file['name'] = substr( $usec, 2 ) . '.' . $file_ext;

		$upload_file['filename'] = $upload_path . $upload_file['name'];
		$short_path = $m_dir . $upload_file['name'];
		$short_path = preg_replace( "/\..+$/i", "s.jpg", $short_path );

		move_uploaded_file( $upload_file["tmp_name"], $upload_file["filename"] );

		//save two files, orignal file and small file
		//return the file path
		//return STATIC_DOMAIN . 
		$edpimage->SaveAvatar( $upload_file["filename"], false );
		return $short_path;
	}

	private function CreateDir ( $dir ) {
		if( ! is_dir( $dir ) ) {
			$temp = explode( '/', $dir );
			$cur_dir = '';
			for( $i = 0; $i < count( $temp ); $i ++ ) {
				$cur_dir .= $temp[$i] . '/';
				if( ! is_dir( $cur_dir ) )
					@mkdir( $cur_dir );
			}
		}
	}

}

?>
